import request from "@/utils/request";
import { AxiosPromise } from "axios";
import { FactoryBaseInfo } from "@/api/porray/types";

/**
 * 登录API
 *
 * @returns
 * @param name
 * @param pass
 */
export function loginApiPr(name: string, pass: string): AxiosPromise<any> {
  return requestPr(`/login/jwttoken3.0?name=${name}&pass=${pass}`);
}

export function factoryInfoReq(): AxiosPromise<FactoryBaseInfo> {
  return requestPr(
    `user/getInfoByToken?token=${localStorage.getItem("token")}`
  );
}

/*
export function factoryInfoReq(data = {}) {
  return requestPrPost(
    `user/getInfoByToken?token=${localStorage.getItem("token")}`,
    {
      commpanyid: "1809",
      intPageSize: 50,
      key: "",
      page: 1,
    }
  );
}
*/

export function requestPr(url, params = {}): AxiosPromise<any> {
  return request({
    url: extendUrlPr(url),
    params,
    method: "get",
  });
}

export function requestPrPost(url, data = {}): AxiosPromise<any> {
  return request({
    url: extendUrlPr(url),
    data: data,
    method: "post",
  });
}

/*
 * 配置pr跨域前缀
 * */
function extendUrlPr(url) {
  if (!url.startsWith("/")) url = "/" + url;
  url.replace("//", "/");
  return "/pr/api" + url;
}
